<?php
class LobbyCommand{
	//@todo MARC: Write LobbyCommand model
	
	
	/**
	 * Join a Target Battle's staging page
	 *
	 * @param array $request ['B'=BattleId, 'Bn'=BattleName, 'P'=PlayerId, 'Pwd'=Password (Pass as NULL if not required)
	 * @return array ['S'=Success (1=Success,-1=Closed,-2=BadPwd,-3=Full,-4=UnknownError), 'Bn'=BattleName]
	 * Success & errors other than -4, also returns 'ts' latest server UNIX timestamp
	 */
	public static function joinStaging(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");

		//Escape harmful chars ============================================
		//TODO MARC: Take MySQL illegal char escaping into use!
		//$request = Database::EscapeParamArray($request);
		
		//Update Password param to NULL if empty string =======================
		If(array_key_exists('Pwd',$request)){
			If(empty($request['Pwd'])) {
				$password = 'NULL';
			}Else{
				$password = "'".$request['Pwd']."'";
			}
			
		}Else{
			$password = 'NULL';
		}
		
		//Convert request to DB Friendly Order==================================
		$params = array($request['B'], $request['P'], $password);
		$battleName = $request['Bn'];
		
		//Contact DB and execute Proc =====================================
		$db = Database::getInstance();
		$rows = $db->callSP(SiteConfig::sP_Battle_Join, $params);
		$rows =$rows[0];
		
//		//### DEBUG ###
//		$debug = "DEBUG: ";
//		foreach($rows As $key => $value){
//			$debug = $debug."[".$key."]=".print_r($value,true);
//		}
//		throw new exception($debug);
//		//### DEBUG ###

		//VERY basic error checking
		If(!empty($rows)){
			//Convert result to return friendly
			$return = array("S"=>$rows["Success"], "Bn"=>$battleName, "ts"=>$rows["NewTsUNIX"]);
			return $return;
		}Else{
			//FAILED no data returned!
			$failure = array("S"=>-4, "Bn"=>$battleName,);
			return $failure;
		}
	}

	public static function hostStaging(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");

		//Escape harmful chars ============================================
		//TODO MARC: Take MySQL illegal char escaping into use!
		//$request = Database::EscapeParamArray($request);
		
		//Convert request to DB Friendly Order==================================
		$params = array($request['P'], $request['Bn']);
		
		//Contact DB and execute Proc =====================================
		$db = Database::getInstance();
		$rows = $db->callSP(SiteConfig::sP_Battle_Create, $params);
	}
	
	
	public static function pollMainLobby(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");

		//Escape harmful chars ============================================
		//TODO MARC: Take MySQL illegal char escaping into use!
		//$request = Database::EscapeParamArray($request);
		
		//Convert request to DB Friendly Order==================================
		//TODO Finish coding pollMainLobby Getter
		
		//Contact DB and execute Proc =====================================
		//TODO Finish coding pollMainLobby Getter
	}
	
	public static function pollStagingArea(array $request){
				require_once(DirectoryManager::$pathCore."/Database.php");

		//Escape harmful chars ============================================
		//TODO MARC: Take MySQL illegal char escaping into use!
		//$request = Database::EscapeParamArray($request);
		
		//Convert request to DB Friendly Order==================================
		//TODO Finish coding pollStagingArea Getter
		
		//Contact DB and execute Proc =====================================
		//TODO Finish coding pollStagingArea Getter
	}
	
	
	/*
	 * Marc
	 * @todo Talk with Matt about the specifics of the Lobby (Conversation points on next line)
	 * 		-File Structure (Do we want to roll Chat and Battle Commands into this one file?
	 * 		-Armarda Selection
	 * 		-Player Profile (Current Armarda, Points/Res, Ladder Info, Play Stats(Wins/Looses/Draws/Resigns), Motto, Image, Etc)
	 * 		-Chat
	 * 		-Teams
	 * 		-Map Settings (Leave till later when we have coded a random map generator?)
	 *		-Randomly Decide who starts turn first 
	 * 		-Create Battle
	 * 
	 * Idea is to have all generic Lobby commands here
	 * 		-New Lobby
	 * 		-Add Host
	 * 		-Add Participant
	 * 		-Ship Selection 
	 * 		-Clear Chat
	 */
}
?>